package com.example.ecas.controller;

import com.example.ecas.common.CommonResponse;
import com.example.ecas.persistence.CaseMapper;
import com.example.ecas.persistence.PatientMapper;
import com.example.ecas.pojo.Case;
import com.example.ecas.pojo.Disease;
import com.example.ecas.pojo.Patient;
import com.example.ecas.service.CaseService;
//import com.example.ecas.utils.Authentication;
import com.example.ecas.service.DiseaseService;
import com.example.ecas.vo.CaseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@RestController
@Controller
public class CaseController {

//    @Autowired
//    Authentication authentication;

    @Autowired
    CaseMapper caseMapper;

    @Autowired
    PatientMapper patientMapper;

    @Autowired
    CaseService caseService;

    @Autowired
    DiseaseService diseaseService;

    // 返回所有病历 [ 患者名字列表, 病历列表 ]
    @GetMapping(value = "/cases")
    @ResponseBody
    public CommonResponse<Object> getAllCases() {
        List<Case> cases = caseMapper.getAllCases();
        return CommonResponse.creatForSuccess("获取成功", caseService.appendPatientName(cases));
    }

    // 返回指定病历id的病历
    @GetMapping(value = "/case")
    @ResponseBody
    public CommonResponse<Object> getCaseById(@RequestParam("caseId") String id) {
        int caseId;
        try {
            caseId = Integer.parseInt(id);
        } catch (Exception e) {
            return CommonResponse.creatForError("无效的病历ID类型");
        }
        Case _case = caseMapper.getCase(caseId);
        if (_case == null) {
            return CommonResponse.creatForError("查找的病历不存在");
        }

        return CommonResponse.creatForSuccess("获取成功", _case);
    }

    // 返回指定病历id的病历和患者
    @GetMapping(value = "/case/patient")
    @ResponseBody
    public CommonResponse<Object> getcaseAndPatientById(@RequestParam("caseId") String id) {
        int caseId;
        try {
            caseId = Integer.parseInt(id);
        } catch (Exception e) {
            return CommonResponse.creatForError("无效的病历ID类型");
        }
        Case _case = caseMapper.getCase(caseId);
        if (_case == null) {
            return CommonResponse.creatForError("查找的病历不存在");
        }
        List<Case> caseList = new ArrayList<>();
        caseList.add(_case);
        Patient patient = patientMapper.getPatient(caseId);
        if (patient == null) {
            return CommonResponse.creatForError("查找的患者不存在");
        }
        List<String> patientList = new ArrayList<>();
        patientList.add(patient.getName());

        List<Object> objectList = new ArrayList<>();
        objectList.add(caseList);
        objectList.add(patientList);
        return CommonResponse.creatForSuccess("获取成功", objectList);
    }

    // 返回给定id的相似病历
    @GetMapping(value = "/case/similarities")
    @ResponseBody
    public CommonResponse<Object> getSimilarCases(@RequestParam("id") String id) {
        int caseId;
        try {
            caseId = Integer.parseInt(id);
        } catch (Exception e) {
            return CommonResponse.creatForError("无效的病历ID类型");
        }
        List<Case> caseList = caseService.caseRecommend(caseId);
        if(caseList == null || caseList.size() < 1) {
            return CommonResponse.creatForError("相似病历为空");
        }
        List<Object> result = new ArrayList<>();
        List<Object> disList = new ArrayList<>();
        for (Case _case : caseList) {
            disList.add(diseaseService.getDiseasesByCase(_case.getId()));
        }
        result.add(caseList);
        result.add(disList);
        return CommonResponse.creatForSuccess(result);
    }

    // 返回指定患者的所有病历
    @GetMapping(value = "/patientCase")
    @ResponseBody
    public CommonResponse<Object> getCaseByPatient(@RequestParam("keyword") String keyword) {
        caseService.getCasesByPatientKey(keyword);
        return CommonResponse.creatForSuccess(caseService.getCasesByPatientKey(keyword));
    }

    // 病例导入
    @PostMapping(value = "/case")
    @ResponseBody
    public CommonResponse<String> caseInput(@RequestBody CaseVO caseVO, HttpSession session) {
        System.out.println(caseVO.getMain());
        if (caseService.caseInput(caseVO)) {
            return CommonResponse.creatForSuccess("录入成功！");
        }
        return CommonResponse.creatForError("信息有误");
    }

//    @GetMapping(value = "/deleteCase")
//    @ResponseBody
//    public CommonResponse<Object> deleteCaseById(@RequestParam("caseId") String id, HttpSession session) {
//
//    }
}
